package cn.lyjuan.android.mylibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import cn.lyjuan.android.mylibrary.R;

/**
 * 上滑加载
 * Created by chad on 2016/3/27.
 */
public class SlideUploadListView extends ListView implements AbsListView.OnScrollListener
{
    private static final String TAG = SlideUploadListView.class.getSimpleName();

    private Context context;
    private View footer;
    private int footerLayout = R.layout.slide_upload_footer;

    /**
     * 是否启动上拉加载控件功能
     */
    private boolean isSlideLoadStart = false;

    private boolean isLoading = false;// 标识是否正在加载中

    public SlideUploadListView(Context context)
    {
        super(context);
        init(context, null);
    }

    public SlideUploadListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context, attrs);
    }

    public SlideUploadListView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs)
    {
        if (null != attrs)
        {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.slideUpload);
            footerLayout = ta.getResourceId(R.styleable.slideUpload_footerLayout, footerLayout);
        }

        this.context = context;
        LayoutInflater lf = LayoutInflater.from(context);
        this.footer = lf.inflate(footerLayout, null, false);
        footer.setVisibility(View.GONE);

        this.addFooterView(footer);
        this.setOnScrollListener(this);
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState)
    {
        if (!isSlideLoadStart) return;

        int last = this.getLastVisiblePosition();
        int total = this.getCount();
        Log.d(TAG, "onScrollStateChanged view >> " +
                view.getClass().getSimpleName() + " scrollState >> " + scrollState
                + " last >> " + last + " total >> " + total);

        if (scrollState != OnScrollListener.SCROLL_STATE_IDLE) return;

        if (last < total - 1) return;

        if (isLoading) return;


        this.isLoading = true;
        footer.setVisibility(View.VISIBLE);
        loader.loadMore();
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
//        Log.d(TAG, "onScroll first >> " + firstVisibleItem
//                + " vis >> " + visibleItemCount + " total >> " + totalItemCount);
    }

    /**
     * 加载更多
     */
    public interface LoadMore
    {
        void loadMore();
    }

    private LoadMore loader;

    public void setLoader(LoadMore loader)
    {
        this.loader = loader;
    }

    /**
     * 加载完成回调
     */
    public void onLoadComplete()
    {
        this.isLoading = false;
        this.footer.setVisibility(View.GONE);
        Log.d(TAG, "slide upload compiled");
    }

    public boolean isSlideLoadStart()
    {
        return isSlideLoadStart;
    }

    public void setSlideLoadStart(boolean slideLoadStart)
    {
        isSlideLoadStart = slideLoadStart;
    }
}
